package com.lottery.material.controller.portal.area;

import com.lottery.material.controller.base.BaseController;
import com.lottery.material.enumeration.ResultEnum;
import com.lottery.material.resolver.CustomException;
import com.lottery.material.service.portal.config.AreaDictService;
import com.lottery.material.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:    区域--省市区Controller
 * Author:         sunny
 * CreateDate:     2019-05-14 16:34
 */
@RequestMapping("/portal/area")
@Slf4j
@Controller
public class AreaDictController extends BaseController {

    @Autowired
    AreaDictService areaDictService;

    /**
     * description: 生成省市区
     * author sunny
     * createDate 2019-05-15 下午 8:17
     *
     * @param request
     * @return
     */
    /*@RequestMapping(value = "/createArea", method = RequestMethod.POST)
    @ResponseBody
    public String createArea(HttpServletRequest request) {
        String filePath = "area.json";
        List<AreaDict> areaList = FileUtil.getFile(request, filePath);
        if (areaList != null && areaList.size() > 0) {
            for (AreaDict area : areaList) {
                areaDictService.insertArea(area);
            }
        }
        return null;
    }*/

    /**
     * description: 获取省市区
     * author sunny
     * createDate 2019-05-15 下午 8:16
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getArea", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult goFollow(HttpServletRequest request) {
        Map<String, String> reqParamMap = new HashMap<>();
        //解析参数
        if (ParameterUtil.analyticParameter(request, reqParamMap)) {
            throw new CustomException(ResultEnum.PARSE_EXCEPTION);
        }

        ResponseResult respDate = validateEmpty(reqParamMap);
        if (null != respDate) {
            return respDate;
        }

        //签名验证
        ResponseResult responseResult = ParameterUtil.checkSign(reqParamMap);
        if (null != responseResult) {
            return responseResult;
        }

        try {
            return ResponseResult.buildResult(ResultEnum.GET_AREA_SUCCESS, areaDictService.findAllArea());
        } catch (Exception e) {
            log.error("【获取省市区出现异常】:{}", ExceptionUtil.logExceptionStack(e));
            throw new CustomException(ResultEnum.GET_AREA_EXCEPTION);
        }
    }

    @RequestMapping(value = "/testMap", method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult testMap(HttpServletRequest request) {
        Map<String, Object> paramsMap = MapUtil.getParamsMap(request);
        return ResponseResult.buildResult(200, "成功", paramsMap);
    }

    @RequestMapping(value = "/testIp", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult testIp(HttpServletRequest request) {
        Map<String, Object> paramsMap = MapUtil.getParamsMap(request);

        //服务器IP
        String serverIp_1 = IpUtil.getServerIp();
        paramsMap.put("serverIp_1", serverIp_1);

        //服务器IP
        String serverIp_2 = IpUtil.queryServerIp();
        paramsMap.put("serverIp_2", serverIp_2);

        //服务器IP
        String serverIp_3 = IpUtil.findServerIp();
        paramsMap.put("serverIp_3", serverIp_3);

        //客户端IP
        String clientIp_1 = IpUtil.getClientIp(request);
        paramsMap.put("clientIp_1", clientIp_1);

        //客户端IP
        String clientIp_2 = IpUtil.queryClientIp(request);
        paramsMap.put("clientIp_2", clientIp_2);

        //客户端IP
        String clientIp_3 = IpUtil.findClientIp(request);
        paramsMap.put("clientIp_3", clientIp_3);

        return ResponseResult.buildResult(200, "成功", paramsMap);
    }

    @RequestMapping(value = "/testIp2", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult testIp2(HttpServletRequest request) {
        Map<String, Object> paramsMap = MapUtil.getParamsMap(request);
        boolean isIp = IpTool.ipValid("192.168.0.124");
        paramsMap.put("isIp", isIp);

        boolean isIpOrNot = IpTool.ipValid("192.168.0.124.8");
        paramsMap.put("isIpOrNot", isIpOrNot);

        String localIp = IpTool.getLocalIp();
        paramsMap.put("localIp", localIp);

        String clientIp = IpTool.getClientIp(request);
        paramsMap.put("clientIp", clientIp);

        Integer number = IpTool.translateIp2Int("127.0.0.1");
        paramsMap.put("number", number);

        return ResponseResult.buildResult(200, "成功", paramsMap);
    }

}
